package com.cennavi.minemapdemo.activity.basic;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.activity.BaseActivty;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.GeoRect;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.camera.CameraUpdateFactory;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.geometry.LatLngBounds;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;

/**
 * Created by lijingru on 2018/11/7
 */
public class MapCameraTarget extends BaseActivty {

    //地图视图
    private MapView mapView;
    //地图操作类
    private MineMap mineMap;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        MinemapAccountManager.getInstance(this, Config.MAP_TOK, Config.OFFLINE_TOK, Config.MAP_ID);
        setContentView(R.layout.activity_map_padding);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
        //初始化地图
        initMap();
    }

    public void zoomIn(View view){
        if (mineMap != null){
            mineMap.moveCamera(CameraUpdateFactory.zoomIn());
        }
    }

    public void zoomOut(View view){
        if (mineMap != null){
            mineMap.moveCamera(CameraUpdateFactory.zoomOut());
        }
    }

    private void initMap() {

        //mine map sdk 2.0.6之前生效
        // mapView.setStyleUrl(Config.mBaseUrl + Config.MAP_ID);
        mapView.setApiBaseUrl(Config.API_BASE_URL);
        mapView.setApiUrl(Config.API_URL);
        mapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(MineMap oMineMap) {
                mineMap = oMineMap;
                mineMap.getUiSettings().setZoomControlsEnabled(false);
                mineMap.getUiSettings().setRotateGesturesEnabled(false);// 是否可以旋转地图
                mineMap.getUiSettings().setTiltGesturesEnabled(false);

                //mine map sdk 2.0.6及之后版本这样使用
                mineMap.setStyleUrl(Config.BASE_URL + Config.MAP_ID);

                mineMap.setCameraPosition(
                        new CameraPosition.Builder().target(new LatLng(Config.mapCentrelat,Config.mapCentrelon)).zoom(14)
                                .tilt(0)//设定倾斜角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                mineMap.setMaxZoomPreference(17);//设置最高展示级别，最高为17级别
                mineMap.setMinZoomPreference(3);//设置最低的展示级别

            }

        });
    }

    public void onCameratarget(View view){

        double x1 = 123.4418114224643;
        double y1 = 41.810246132876834;
        double x2 = 123.42520318622724;
        double y2 = 41.79582892686385;

        GeoRect routeRect = new GeoRect(x1, y1, x1, y1);
        routeRect.extend(new LatLng(y2,x2));

        if (mineMap != null){
            LatLng NW = new LatLng(routeRect.getTop(), routeRect.getLeft());
            LatLng SE = new LatLng(routeRect.getBottom(), routeRect.getRight());
            LatLngBounds latLngBounds = new LatLngBounds.Builder().include(NW).include(SE).build();
            mineMap.setLatLngBoundsForCameraTarget(latLngBounds);
        }
    }

    //生命周期
    @Override
    protected void onStart() {
        super.onStart();
        if (mapView != null)
            mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null)
            mapView.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mapView != null)
            mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapView != null)
            mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (mapView != null){
            mapView.onLowMemory();
        }
    }

}
